package com.innovation.chengzhangriji.ui.adapter;

import android.graphics.Color;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.model.UIHistoryVideo;
import com.innovation.chengzhangriji.util.ChZhRJUtils;
import com.innovation.chengzhangriji.util.Size;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class HistoryVideoContentHolder extends RecyclerView.ViewHolder {

    // 列数
    public static final int COLUMN_NUM = 3;
    // 列的间距 (dp)
    private static final int COLUMN_SPACE = 8;
    // 行的间距 (dp)
    private static final int ROW_SPACE = 8;
    // 单个视频的大小
    private Size size;
    private DisplayMetrics metrics;
    // 点击监听器
    private VideoClickListener clickListener;

    public HistoryVideoContentHolder(View itemView, VideoClickListener clickListener) {
        super(itemView);
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
                RecyclerView.LayoutParams.WRAP_CONTENT);
        itemView.setLayoutParams(params);
        size = computeCellSize();
        this.clickListener = clickListener;
    }

    // 计算列宽
    private Size computeCellSize() {
        metrics = itemView.getResources().getDisplayMetrics();
        float width = (metrics.widthPixels - metrics.density * (COLUMN_SPACE + 10) * 2) / COLUMN_NUM;
        float height = width * 0.76f;
        return new Size((int)width, (int)height);
    }

    // 放置视频列表
    public void setVideoList(List<UIHistoryVideo> videoList) {
        ((RelativeLayout)itemView).removeAllViews();
        for(int i = 0; i < videoList.size(); i++) {
            VideoViewHolder holder = createVideoView(i);
            holder.durationView.setText(videoList.get(i).getDuration());
            if(videoList.get(i).getThumbnailUrl().startsWith("/")) {
                ImageLoader.getInstance().displayImage("file://" + videoList.get(i).getThumbnailUrl(), holder.roundedImageView, Config.getOptionsPhoto());
            } else {
                ImageLoader.getInstance().displayImage(videoList.get(i).getThumbnailUrl(), holder.roundedImageView, Config.getOptionsPhoto());
            }
            holder.itemLayout.setTag(R.id.diaryId, videoList.get(i).getDiaryId());
            holder.itemLayout.setTag(R.id.url, videoList.get(i).getUrl());
            holder.itemLayout.setTag(R.id.localUrl, videoList.get(i).getLocalUrl());
            ((RelativeLayout)itemView).addView(holder.itemLayout);
        }
    }

    // 创建单个视频
    private VideoViewHolder createVideoView(int position) {
        int rowIndex = position / COLUMN_NUM;
        int colIndex = position % COLUMN_NUM;
        RelativeLayout itemLayout = new RelativeLayout(itemView.getContext());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(size.getWidth(), size.getHeight());
        int left = (int) (colIndex * (size.getWidth() + metrics.density * COLUMN_SPACE));
        int top = (int) (rowIndex * (size.getHeight() + metrics.density * ROW_SPACE));
        params.setMargins(left, top, 0, 0);
        itemLayout.setLayoutParams(params);
        // 视频图片
        ImageView imageView = new ImageView(itemView.getContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        RelativeLayout.LayoutParams p3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(p3);
        itemLayout.addView(imageView);
        // 视频图标
        ImageView videoIcon = new ImageView(itemView.getContext());
        videoIcon.setImageResource(R.mipmap.icon_history_video);
        RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        p1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        p1.setMargins((int) metrics.density * 5, 0, 0, (int) metrics.density * 5);
        videoIcon.setLayoutParams(p1);
        itemLayout.addView(videoIcon);
        // 视频时长
        TextView videoDurationText = new TextView(itemView.getContext());
        videoDurationText.setTextSize(12);
        videoDurationText.setTextColor(Color.WHITE);
        RelativeLayout.LayoutParams p2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        p2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        p2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
        p2.setMargins(0, 0, (int) metrics.density * 5, (int) metrics.density * 5);
        videoDurationText.setLayoutParams(p2);
        itemLayout.addView(videoDurationText);
        return new VideoViewHolder(itemLayout, imageView, videoDurationText);
    }

    /**
     * 点击事件
     */
    public interface VideoClickListener  {

        void onVideoClick(String diaryId, String url, String localUrl);
    }

    // 单个视频的 Holder
    class VideoViewHolder implements View.OnClickListener {

        // 单个视频
        public RelativeLayout itemLayout;
        // 视频截图
        public ImageView roundedImageView;
        // 视频时长
        public TextView durationView;

        public VideoViewHolder(RelativeLayout itemLayout, ImageView roundedImageView, TextView durationView) {
            this.itemLayout = itemLayout;
            this.roundedImageView = roundedImageView;
            this.durationView = durationView;
            this.itemLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            clickListener.onVideoClick((itemLayout.getTag(R.id.diaryId) != null? itemLayout.getTag(R.id.diaryId).toString() : ""),
                    (itemLayout.getTag(R.id.url) != null? itemLayout.getTag(R.id.url).toString() : ""),
                    (itemLayout.getTag(R.id.localUrl) != null? itemLayout.getTag(R.id.localUrl).toString() : ""));
        }
    }
}
